import { contextBridge, ipcRenderer } from 'electron'
import type { CustomScreen } from './electron-api'

// Custom APIs for renderer
const api: CustomScreen = {
  getScreenSource() {
    return ipcRenderer.invoke('GET_SCREEN_SOURCE')
  }
}

// Use `contextBridge` APIs to expose Electron APIs to
// renderer only if context isolation is enabled, otherwise
// just add to the DOM global.
if (process.contextIsolated) {
  try {
    contextBridge.exposeInMainWorld('api', api)
    console.log('register in contextBridge')
  } catch (error) {
    console.error(error)
  }
} else {
  // @ts-ignore (define in dts)
  window.api = api
}
